/**
 * Created by sky on 16/5/30.
 *
 * @param $scope
 * @param $rootScope
 * @param AUTH_EVENTS   登录状态  ../src/login/userStation
 * @param loginservice  登录服务  ../src/login/loginservice
 */
/* @ngInject */
module.exports = function ($rootScope,$scope,$cookies,$state,loginService,registerService,httpservice){
    $rootScope.loadingShow = false;
    $rootScope.fadeShow = false;
    $scope.phone = '';
    $scope.phonePwd = '';
    $scope.checkCode = '';
    $scope.picCode = '';
    // 图片验证码
    $scope.picVerify = false;
    // // 手机验证码
    // $scope.phoneVerify = false;
    // 账户改变
    $scope.accountChange = accountChange;
    // $scope.reloadPicCode = reloadPicCode;

    /*用户Id和pwd的默认缓存时间*/
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 1);

    var emailRe = '';/*验证邮箱的正则表达式*/
    var phoneRe = '';/*验证手机号的正则表达式*/
    /*初始化函数*/
    $scope.init = function(){
        /*判断用户名是否有*/
        if($cookies.get('userIdBenDi') == null|| $cookies.get('userIdBenDi') == ''||$cookies.get('userIdBenDi') == undefined){

        }else{
            $scope.phone =  kbt.decrypt($cookies.get('userIdBenDi'));
        }
        /*判断是否记住密码*/
        if($cookies.get('isPwd')  == null || $cookies.get('isPwd')  ==  ''||$cookies.get('isPwd')  == undefined||$cookies.get('isPwd')  =='false'){
            $scope.phonePwd = '';
        }else{
            $('#rePwd input').attr('checked','checked');
            $scope.phonePwd =  kbt.decrypt($cookies.get('PwdBenDi'));
            //勾选制动登陆
            //$scope.login();
        }

        $scope.checkCode = {
          //.slice(0,httpservice.getPicVerifyCodeUrl.indexOf('?'))
          img:httpservice.getPicVerifyCodeUrl+ '?' + new Date().getTime(),//校验码图片,
          code: null,//校验码
          verify: 0//验证状态
        };
    };

    $scope.checkPhoneOrEmail = function () {
        if($scope.phone != ''){
            if($scope.phone.indexOf("@")>=0){
                if(CheckMail($scope.phone)){
                    $rootScope.loadingShow = true;
                    $rootScope.fadeShow = true;
                    //检查邮箱是否已经注册
                    registerService.getRegisterEmail($scope.phone).then(function (response) {
                        $rootScope.loadingShow = false;
                        $rootScope.fadeShow = false;
                        if (response.code == 1) {
                            $('.error_num').fadeIn();
                            $('.error_num').children('p').html("该账号未注册，请先注册");
                        }else {
                            $('.error_num').fadeOut();
                            $scope.login();
                        }
                    }, function (response) {
                        console.error('getBindPhone error', response.status, response.statusText);
                    });
                }else {
                    $('.error_num').fadeIn();
                    $('.error_num').children('p').html("账号不正确");
                }

            }else {
                if(checkPhone($scope.phone)){
                    $rootScope.loadingShow = true;
                    $rootScope.fadeShow = true;
                    //检查手机是否已经注册
                    registerService.getRegisterPhone($scope.phone).then(function (response) {
                        $rootScope.loadingShow = false;
                        $rootScope.fadeShow = false;
                        if (response.code == 1) {
                            $('.error_num').fadeIn();
                            $('.error_num').children('p').html("该账号未注册，请先注册");
                        }else {
                            $('.error_num').fadeOut();
                            $scope.login();
                        }
                    }, function (response) {
                       // console.error('getBindPhone error', response.status, response.statusText);
                    });

                }else {
                    $('.error_num').fadeIn();
                    $('.error_num').children('p').html("账号不正确");
                }
            }
        }else {
            $('.error_num').fadeIn();
            $('.error_num').children('p').html("账号不正确");
        }
    };


    //点击登陆
    $scope.login = function () {
        //$state.go('workBarNoWork');
        //错误提示清除
        $('.error_pwd').fadeOut();
        $('.error_num').fadeOut();

        if ($scope.phone.indexOf("@") >= 0) {
            var num = 2;
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            loginService.login('', $scope.phone, $scope.phonePwd, num, $scope.picCode).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;



                $scope.picVerify = data.data.picVerifyShow;
                // $scope.phoneVerify = data.data.phoneVerifyShow;

                if (data.code == 1) {
                    $scope.headImageUrl = data.data.iconUrl;
                    $rootScope.startWebSocket(data.data.id);
                    /*本地缓存用户Id*/
                    var userIdBenDi = kbt.encrypt($scope.phone);
                    $cookies.put('userIdBenDi', userIdBenDi, {
                        expires: expireDate
                    });

                    /*本地缓存Pwd*/
                    if ($cookies.get('isPwd') == 'true') {
                        var PwdBenDi = kbt.encrypt($scope.phonePwd);
                        $cookies.put('PwdBenDi', PwdBenDi, {
                            expires: expireDate
                        });
                    } else {
                        $cookies.remove('PwdBenDi');
                    }

                    /*本地保存用户id，用户名，isUpdated,token*/
                    $cookies.put('cookieUserId', kbt.encrypt(data.data.id), {
                        expires: expireDate
                    });
                    $cookies.put('cookieUserName', kbt.encrypt(data.data.username), {
                        expires: expireDate
                    });
                    $cookies.put('cookieIsUpdated', data.data.isUpdated, {
                        expires: expireDate
                    });
                    $cookies.put('cookieToken', data.conditions, {
                        expires: expireDate
                    });
                    $cookies.put('userPortraitSave', data.data.iconUrl,{
                      expires:expireDate
                    });
                    // 登录拦截
                    $cookies.put('userIdBenDiFilter', userIdBenDi);

                    // 左侧菜单位置清空
                    $cookies.put('leftBarIndex', '');

                    $state.go('workBarNoWork');

                } else {
                    $('.error_num').fadeIn();
                    $('.error_num').children('p').html(data.msg);
                    $scope.reloadPicCode();
                }
            }, function () {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
            });
        } else {
            var num = 1;
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            loginService.login($scope.phone, '', $scope.phonePwd, num, $scope.picCode).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;

               // $scope.picVerify = data.data.picVerifyShow;
                // $scope.phoneVerify = data.data.phoneVerifyShow;

                $scope.picVerify = data.data.picVerifyShow;
                if (data.code == 1) {
                    $rootScope.startWebSocket(data.data.id);

                    /*本地缓存用户Id*/
                    var userIdBenDi = kbt.encrypt($scope.phone);
                    $cookies.put('userIdBenDi', userIdBenDi, {
                        expires: expireDate
                    });

                    /*本地缓存Pwd*/
                    if ($cookies.get('isPwd') == 'true') {
                        var PwdBenDi = kbt.encrypt($scope.phonePwd);
                        $cookies.put('PwdBenDi', PwdBenDi, {
                            expires: expireDate
                        });
                    } else {
                        $cookies.remove('PwdBenDi');
                    }

                    /*本地保存用户id，用户名，isUpdated,token*/
                    $cookies.put('cookieUserId', kbt.encrypt(data.data.id), {
                        expires: expireDate
                    });
                    $cookies.put('cookieUserName', kbt.encrypt(data.data.username), {
                        expires: expireDate
                    });
                    $cookies.put('cookieIsUpdated', data.data.isUpdated, {
                        expires: expireDate
                    });
                    $cookies.put('cookieToken', data.conditions, {
                        expires: expireDate
                    });

                    // 登录拦截
                    $cookies.put('userIdBenDiFilter', userIdBenDi);
                    $state.go('workBarNoWork');

                } else {
                    $('.error_num').fadeIn();
                    $('.error_num').children('p').html(data.msg);
                    $scope.reloadPicCode();

                }
            }, function () {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
            });
        }
    };
    function accountChange() {
        // 图片验证码
        $scope.picVerify = false;
        // // 手机验证码
        // $scope.phoneVerify = false;
    }
    /*记住,删除密码的状态*/
    $('#rePwd').change(function(){
        if( $('#rePwd input').prop('checked')){
            /*用于判断是否是记住密码的状态*/
            $scope.isPwd = true;
            $cookies.put('isPwd',$scope.isPwd,{
                'expires':expireDate
            });
        }else{
            $scope.isPwd = false;
            $cookies.put('isPwd',$scope.isPwd,{
                'expires':expireDate
            });
        }
    });
    $scope.init();/*初始化函数*/


    //获取验证码图片
    $scope.reloadPicCode = function() {
        $scope.checkCode.img = httpservice.getPicVerifyCodeUrl + '?' + new Date().getTime();
    }

    $scope.changePwdCode = function(){
      if($scope.phonePwd!==""){
        if ($("#phonepwd").attr("type") == "password") {
          $("#eye").attr("src", "../assets/img/eye-on.svg");
          $("#phonepwd").attr("type", "text")
        }
        else {
          $("#eye").attr("src", "../assets/img/eye-off.svg");
          $("#phonepwd").attr("type", "password")
        }

      }else{

      }

    }

};
